<?php
/**
 * @todo 导出xsl
 * @copyright gaoxiang 2009
 * @author gaoxiang
 * @createdate 2009.04.19
 * @license 本程序遵照Creative Commons 署名-非商业性使用-相同方式共享 2.5 中国大陆 协议，该协议的中文版本http://creativecommons.org/licenses/by-nc-sa/2.5/cn/
 **/
$check_online=1;
require_once(dirname(dirname(__FILE__)) . '/require/load.php');
require_once('PHPExcel.php');
require_once('PHPExcel/Writer/Excel5.php');
require_once ('PHPExcel/IOFactory.php');
session_start();
check_user();
//把请求内容写入数据库，以备客户端查证
if (isset($_POST['content'])){
	$_SESSION['output']=0;
}else
	exit;
// 创建一个处理对象实例
set_time_limit(0);
$objExcel = new PHPExcel();

//设置文档基本属性

$objProps = $objExcel->getProperties();
$objProps->setCreator("Gaoxiang");
$objProps->setLastModifiedBy("Gaoxiang");
$objProps->setTitle("Office XLS Document");
$objProps->setSubject("Office XLS Document, aluminum");
$objProps->setDescription("Generated byGdcopper Aluminum Tube Division Data System.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("aluminum tube");
//*************************************

//设置当前的sheet索引，用于后续的内容操作。

//一般只有在使用多个sheet的时候才需要显示调用。
$objExcel->setActiveSheetIndex(0);
//缺省情况下，PHPExcel会自动创建第一个sheet被设置SheetIndex=0 
$objActSheet = $objExcel->getActiveSheet();
  
//设置当前活动sheet的名称

$objActSheet->setTitle('output');
  
//*************************************
//设置所有单元格都水平垂直居中 
$objActSheet->getPageSetup()->setHorizontalCentered(true);
$objActSheet->getPageSetup()->setVerticalCentered(true);
//设置单元格内容 

//
if ($_GET['action']=='product'){
//由PHPExcel根据传入内容自动判断单元格内容类型
$objActSheet->setCellValue('A1', '序号');
$objActSheet->setCellValue('B1', '日期'); 
$objActSheet->setCellValue('C1', '投料批号');
$objActSheet->setCellValue('D1', '客户');
$objActSheet->setCellValue('E1', '规格');
$objActSheet->setCellValue('F1', '产品批号');
$objActSheet->setCellValue('G1', '重量');
$objActSheet->setCellValue('H1', '模具');
$objActSheet->setCellValue('I1', '伤点');
$objActSheet->setCellValue('J1', '表面');
$objActSheet->setCellValue('K1', '判定结果');
$objActSheet->setCellValue('L1', '备注');

//设置宽度

$objActSheet->getColumnDimension('A')->setAutoSize(true);
$objActSheet->getColumnDimension('B')->setAutoSize(true);
$objActSheet->getColumnDimension('C')->setAutoSize(true);
$objActSheet->getColumnDimension('D')->setAutoSize(true);
$objActSheet->getColumnDimension('E')->setAutoSize(true);
$objActSheet->getColumnDimension('F')->setAutoSize(true);
$objActSheet->getColumnDimension('G')->setAutoSize(true);
$objActSheet->getColumnDimension('H')->setAutoSize(true);
$objActSheet->getColumnDimension('I')->setAutoSize(true);
$objActSheet->getColumnDimension('J')->setAutoSize(true);
$objActSheet->getColumnDimension('K')->setAutoSize(true);
$objActSheet->getColumnDimension('L')->setAutoSize(true);
//************************************* 
//从数据库导出数据到A2-...
$content=explode('|',$_POST['content']);
	for($i=0;$i<=count($content);++$i){
		$query="SELECT pcreate_time,pclient,pspec,prog,product_id,pweight,pmould,pdefect,psurface,pjudge,premarks FROM produce where product_id='" . $content[$i] ."' LIMIT 1";
		$result=mysql_query($query);
		while($arr_result=mysql_fetch_array($result)){
         $objActSheet->setCellValue('A' . ($i+2), $i+1);
         $objActSheet->setCellValue('B' . ($i+2), substr($arr_result['pcreate_time'],0,10)); 
         $objActSheet->setCellValue('C' . ($i+2), $arr_result['prog']);
         $objActSheet->setCellValue('D' . ($i+2), $arr_result['pclient']);
         $objActSheet->setCellValue('E' . ($i+2), $arr_result['pspec']);
         $objActSheet->setCellValue('F' . ($i+2), $arr_result['product_id']);
         $objActSheet->setCellValue('G' . ($i+2), $arr_result['pweight']);
         $objActSheet->setCellValueExplicit('H' . ($i+2), $arr_result['pmould'],PHPExcel_Cell_DataType::TYPE_STRING);
         $objActSheet->setCellValue('I' . ($i+2), $arr_result['pdefect']);
         $objActSheet->setCellValue('J' . ($i+2), $arr_result['psurface']);
         $objActSheet->setCellValue('K' . ($i+2), $arr_result['pjudge']);
         $objActSheet->setCellValue('L' . ($i+2), $arr_result['premarks']);
		}
	}
$_SESSION['output']=1;//修改output变量为1，表示已经生成完成，然后通过check.php通知客户端
//输出内容到浏览器
header('Content-Type: application/vnd.ms-excel;');
header('Content-Disposition: attachment;filename=output.xls');
header('Cache-Control: max-age=0');
//创建文件格式写入对象实例
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');
}elseif($_GET['action']=='rog'){
	//由PHPExcel根据传入内容自动判断单元格内容类型 
$objActSheet->setCellValue('A1', '序号');
$objActSheet->setCellValue('B1', '日期'); 
$objActSheet->setCellValue('C1', '厂家');
$objActSheet->setCellValue('D1', '牌号');
$objActSheet->setCellValue('E1', '规格');
$objActSheet->setCellValue('F1', '重量');
$objActSheet->setCellValue('G1', '供货批号');
$objActSheet->setCellValue('H1', '原料批号');
$objActSheet->setCellValue('I1', '抗拉强度');
$objActSheet->setCellValue('J1', '伸长率');
$objActSheet->setCellValue('K1', '退火批号');
$objActSheet->setCellValue('L1', '退火工艺');
$objActSheet->setCellValue('M1', '抗拉强度');
$objActSheet->setCellValue('N1', '伸长率');
$objActSheet->setCellValue('O1', '判定结果');
$objActSheet->setCellValue('P1', '备注'); 
//设置宽度 

$objActSheet->getColumnDimension('A')->setAutoSize(true);
$objActSheet->getColumnDimension('B')->setAutoSize(true);
$objActSheet->getColumnDimension('C')->setAutoSize(true);
$objActSheet->getColumnDimension('D')->setAutoSize(true);
$objActSheet->getColumnDimension('E')->setAutoSize(true);
$objActSheet->getColumnDimension('F')->setAutoSize(true);
$objActSheet->getColumnDimension('G')->setAutoSize(true);
$objActSheet->getColumnDimension('H')->setAutoSize(true);
$objActSheet->getColumnDimension('I')->setAutoSize(true);
$objActSheet->getColumnDimension('J')->setAutoSize(true);
$objActSheet->getColumnDimension('K')->setAutoSize(true);
$objActSheet->getColumnDimension('L')->setAutoSize(true);
$objActSheet->getColumnDimension('M')->setAutoSize(true);
$objActSheet->getColumnDimension('N')->setAutoSize(true);
$objActSheet->getColumnDimension('O')->setAutoSize(true);
//*************************************
//从数据库导出数据到A2-...
$content=explode('|',$_POST['content']);
	for($i=0;$i<=count($content);++$i){
		$query="select stockdate,supplier,make,spec,weight,origin,raw,tensile,extend,anneal,technics,atensile,aextend,judge,remarks from rog where raw='" . $content[$i] ."' LIMIT 1";
		$result=mysql_query($query);
		while($arr_result=mysql_fetch_array($result)){
         $objActSheet->setCellValue('A' . ($i+2), $i+1);
         $objActSheet->setCellValue('B' . ($i+2), $arr_result['stockdate']);
         $objActSheet->setCellValue('C' . ($i+2), $arr_result['supplier']);
         $objActSheet->setCellValue('D' . ($i+2), $arr_result['make']);
         $objActSheet->setCellValue('E' . ($i+2), $arr_result['spec']);
         $objActSheet->setCellValue('F' . ($i+2), $arr_result['weight']);
         $objActSheet->setCellValueExplicit('G' . ($i+2), $arr_result['origin'],PHPExcel_Cell_DataType::TYPE_STRING);
         $objActSheet->setCellValue('H' . ($i+2), $arr_result['raw']);
         $objActSheet->setCellValue('I' . ($i+2), $arr_result['tensile']);
         $objActSheet->setCellValue('J' . ($i+2), $arr_result['extend']);
         $objActSheet->setCellValue('K' . ($i+2), $arr_result['anneal']);
         $objActSheet->setCellValue('L' . ($i+2), $arr_result['technics']);
         $objActSheet->setCellValue('M' . ($i+2), $arr_result['atensile']);
		 $objActSheet->setCellValue('N' . ($i+2), $arr_result['aextend']);
		 $objActSheet->setCellValue('O' . ($i+2), $arr_result['judge']);
		 $objActSheet->setCellValue('P' . ($i+2), $arr_result['remarks']);
		}
	}
$_SESSION['output']=1;//修改output变量为1，表示已经生成完成，然后通过check.php通知客户端
//输出内容到浏览器
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment;filename="output.xls"');
header('Cache-Control: max-age=0');
//创建文件格式写入对象实例
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save('php://output');
}
?>